<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>generator的应用</title>
    <script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
</head>
<body>
<script>

    // Generator部署ajax操作，让异步代码同步化

    /* function* main() {
         let res = yield  request('https://mock.presstime.cn/mock/6360bd8828238a008887296c/demo/query')
         console.log(res)

         console.log('数据请求完成，可以进行后续操作')
     }

     const ite = main();
     ite.next();

     function request(url) {
         $.ajax({
             url,
             method: 'get',
             success(res) {
                 ite.next(res)
             }
         })
     }*/

    function* load() {
        loadUI()
        yield showData()
        hideUI()
    }

    let itLoad = load();
    itLoad.next()

    function loadUI() {
        console.log('加载loading...界面')
    }

    function hideUI() {
        console.log('隐藏loading...界面')
    }

    function showData() {
        // 模拟异步操作
        setTimeout(() => {
            console.log('数据加载完成')
            itLoad.next()
        }, 1000)
    }

</script>
</body>
</html>
